package com.giscloud.amp.service.sys.impl;

import com.alibaba.fastjson.JSON;
import com.giscloud.amp.dao.AmpLoginLogMapper;
import com.giscloud.amp.dao.AmpStaffMapper;
import com.giscloud.amp.entity.AmpLoginLog;
import com.giscloud.amp.entity.AmpLoginLogExample;
import com.giscloud.amp.entity.AmpStaffExample;
import com.giscloud.amp.entity.dto.LoginLogDTO;
import com.giscloud.amp.entity.vo.AmpLoginLogVO;
import com.giscloud.amp.entity.vo.AmpStaffVO;
import com.giscloud.amp.service.sys.LoginLogService;
import com.giscloud.commons.model.Result;
import com.giscloud.datasource.page.PageInfo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:gxd
 * @Description:
 * @Date: 11:22 2018/2/26
 * @Modified By:
 */
@Service("loginLogService")
public class LoginLogServiceImpl implements LoginLogService {
    private final static Logger log = LoggerFactory.getLogger(LoginLogServiceImpl.class);
    @Autowired
    private AmpLoginLogMapper ampLoginLogMapper;
    @Autowired
    private AmpStaffMapper ampStaffMapper;

    @Override
    public PageInfo getList(LoginLogDTO loginLogDTO) {
        log.debug("#getLoginLogList parameter ={}", JSON.toJSONString(loginLogDTO));
        PageHelper.startPage(loginLogDTO.getPage(), loginLogDTO.getRows());
        AmpLoginLogExample ampLoginLogExample = new AmpLoginLogExample();
        AmpLoginLogExample.Criteria criteria = ampLoginLogExample.createCriteria();
        if(loginLogDTO.getLoginStaffId() != null){
        	criteria.andLoginStaffIdEqualTo(loginLogDTO.getLoginStaffId());
        }
        if(loginLogDTO.getSystemId() != null){
            criteria.andSystemIdEqualTo(loginLogDTO.getSystemId());
        }
        if(loginLogDTO.getStartDate() != null && loginLogDTO.getEndDate() != null){
            criteria.andLoginDateBetween(loginLogDTO.getStartDate(),loginLogDTO.getEndDate());
        }
        AmpLoginLogExample.Criteria criteria2 = ampLoginLogExample.createCriteria();
        if(loginLogDTO.getLoginStaffId() != null){
        	criteria2.andLoginStaffIdEqualTo(loginLogDTO.getLoginStaffId());
            //criteria2.andLogIdEqualTo(loginLogDTO.getLoginStaffId());
        }
        if(loginLogDTO.getLoginOutStartDate() != null && loginLogDTO.getLoginOutEndDate() != null){
            criteria2.andLogoutDateBetween(loginLogDTO.getLoginOutStartDate(),loginLogDTO.getLoginOutEndDate());
        }

        ampLoginLogExample.setOrderByClause("LOGIN_DATE DESC");
        ampLoginLogExample.or(criteria2);

        List<AmpLoginLogVO> ampLoginLogVOs = ampLoginLogMapper.selectByExample(ampLoginLogExample);
        AmpStaffExample ampStaffExample = new AmpStaffExample();
        List<AmpStaffVO> ampStaffVOS = ampStaffMapper.selectByExampleWhere(ampStaffExample);
        Map staffNameMap = new HashMap();
        for (AmpStaffVO ampStaffVO : ampStaffVOS) {
            staffNameMap.put(ampStaffVO.getStaffId(),ampStaffVO.getStaffName());
        }
        for (AmpLoginLogVO ampLoginLogVO : ampLoginLogVOs) {
            if(staffNameMap.get(ampLoginLogVO.getLoginStaffId()) != null) {
                ampLoginLogVO.setStaffName(staffNameMap.get(ampLoginLogVO.getLoginStaffId()).toString());
            }
        }
        return PageInfo.of(ampLoginLogVOs);
    }


    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    @Override
    public int delete(Long logId) {
        AmpLoginLogExample ampLoginLogExample = new AmpLoginLogExample();
        AmpLoginLogExample.Criteria criteria = ampLoginLogExample.createCriteria();
        if (logId != null) {
            criteria.andLogIdEqualTo(logId);
        }
        return ampLoginLogMapper.deleteByExample(ampLoginLogExample);
    }
    /**
     * 保存
     */
    @Override
    @Transactional
    @Async
    public Result save(AmpLoginLog ampLoginLog){
        log.debug("#save parameter ={}", JSON.toJSONString(ampLoginLog));
        if(ampLoginLogMapper.insertSelective(ampLoginLog) > 0 ){
            return Result.success("保存成功");
        }else{
            return Result.error("保存失败");
        }
    }

}
